package com.kele.controller;

import com.kele.domain.Blog;
import com.kele.domain.User;
import com.kele.service.BlogService;
import com.kele.service.UserService;
import com.kele.utils.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Controller
@RequestMapping("/blog")
@Api(value = "博客模块", tags = {"博客类管理接口"})
public class BlogController {


    @Resource
    private BlogService blogService;
    @Resource
    private UserService userService;

    /**
     * 发布博客
     * @author 栗守佳
     * @param blog
     * @return
     * @author 栗守佳
     */
    @RequestMapping(value = "/releaseBlog", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "发布博客", notes = "发布博客类基本属性")
    @ApiImplicitParam(name = "blog", value = "博客类")
    public ResponseData releaseBlog(Blog blog, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("User");
        blog.setBlogger(user.getNickname());
        if (blogService.addBlog(blog) > 0) {
            return ResponseData.success("发布成功", blog);
        } else {
            return ResponseData.fail("发布失败");
        }
    }


    /**
     * 展示博客到主页面上
     *
     * @param model 转发list集合
     * @return 返回一个集合 包含着要展示的数据
     * @author 赵奥林
     */
    @RequestMapping(value = "/showBlogs", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "将博客显示到页面上", notes = "随机选取六篇博客显示")
    public ResponseData showBlogs(Model model) {
        //储存博客到集合中
        List<Blog> blogs = blogService.showBlogs();
        //新的list集合储存取出来的数据
        List<Blog> list = new ArrayList<Blog>();
        if (blogs.size() >= 6) {
            Random random = new Random();
            for (int i = 0; i < 6; i++) {
                int intRandom = random.nextInt(blogs.size() - 1);
                list.add(blogs.get(intRandom));
                blogs.remove(blogs.get(intRandom));
            }
        }
        //防止博客篇数少于六篇
        model.addAttribute("blogs", list);
        return ResponseData.success("查询成功", list);
    }

    /**
     * @author 赵奥林
     * @param id
     * @return
     */
    @RequestMapping(value = "/findById", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询一篇博客", notes = "根据id查询博客所有属性")
    @ApiImplicitParam(name = "id", value = "博客id", required = true)
    public ResponseData findBlogs(int id) {
        Blog blog = blogService.findBlogById(id);
        return ResponseData.success("查询成功", blog);
    }

    /**
     * @author 王箫凯
     * @param bid
     * @param request
     * @return
     */
    @ResponseBody
    @ApiOperation(value = "使文章通过审核", notes = "根据博客ID进行审核")
    @ApiImplicitParam(name = "bid", value = "博客id", required = true)
    @RequestMapping(value = "/acceptBlog", method = RequestMethod.POST)
    public ResponseData acceptBlog(int bid, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("User");
        System.out.println(user);
        if (user.getVip() >= 2) {
            //说明为管理员,继续以下操作
            Blog blog = blogService.findBlogById(bid);
            if (blog.getFlag() == 1) {
                return ResponseData.fail("该博客不在审核列表中，请注意查询");
            } else {
                int nums = blogService.executeBlog(bid, 1);
                System.out.println(nums);
                if (nums > 0) {
                    return ResponseData.success("已通过审核");
                }
                return ResponseData.fail("操作失败");
            }
        }return ResponseData.fail("对不起，该方法未找到");

    }

    /**
     * @author 王箫凯
     * @param bid
     * @param request
     * @return
     */
    @PostMapping(value = "/refuseBlog")
    @ResponseBody
    @ApiOperation(value = "将一个文章退回，即发布失败", notes = "根据博客标识 bid 进行操作")
    @ApiImplicitParam(name = "bid", value = "博客的唯一标识--bid", required = true)
    public ResponseData refuseBlog(int bid, HttpServletRequest request) {

        User user = (User) request.getSession().getAttribute("User");
        if (user.getVip() >= 2) {
            //说明为管理员,继续以下操作
            Blog blog = blogService.findBlogById(bid);
            if (blog == null) {
                return ResponseData.fail("该博客不存在");
            }
            if (blog.getFlag() == 2) {
                return ResponseData.fail("该博客不在审核列表中，请注意查询");
            } else {
                int nums = blogService.executeBlog(bid, 2);
                if (nums > 0) {
                    return ResponseData.success("该博客已被退回");
                }
                return ResponseData.fail("操作失败");
            }
        }
        return ResponseData.fail("错误的操作");

    }


    /**
     * 批量删除博客
     * @author 赵奥林
     * @param
     * @return
     */
    @RequestMapping(value = "/deleteArray", method = RequestMethod.DELETE)
    @ApiOperation(value = "批量删除博客", notes = "根据id数组删除博客")
    @ResponseBody
    @ApiImplicitParams(
            @ApiImplicitParam(value = "id数组", name = "bids", required = true)
    )
    public ResponseData deleteList(@RequestParam(value = "bids") Integer[] bids) {
        blogService.deleteArray(bids);
        return ResponseData.success("删除成功");
    }


    /**
     * @author 王箫凯
     * @param account
     * @return
     */
    @RequestMapping(value = "/allInformation",method = RequestMethod.POST)
    @ApiOperation(value = "一个用户查询", notes = "用户查询")
    @ResponseBody
    public ResponseData userCounter(String account) {
        //根据用户账号查询用户的所有信息
        User user = userService.queryByAccount(account);
        //获取该用户的所有文章
        String blogger = user.getNickname();
        List<Blog> list = blogService.findBlogsByBlogger(blogger);
        return ResponseData.success("成功", list);
    }
    @RequestMapping(value = "isVIP",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "VIP阅读",notes = "阅读判断")
    public ResponseData isVip(@RequestParam("bid")int bid,@RequestParam("id") int id,HttpServletRequest request){

        Blog blog=blogService.findBlogById(bid);
        if (blog.getVip()<1){
            return ResponseData.success("可以阅读");
        }
        if(request.getSession().getAttribute("User")==null){
            return ResponseData.fail("VIP文章，请登陆后在进行相关操作");
        }
        User user= (User) request.getSession().getAttribute("User");
        if (blog.getVip()>=1 && user.getVip()>=1){
            return ResponseData.success("可以阅读");

        }
        return ResponseData.fail("不可阅读");
    }
    /**
     * 后台管理博客模糊查询
     * @param attribute
     * @param model
     * @return
     */
    @RequestMapping(value = "/fuzzyBlog",method = RequestMethod.GET)
    @ApiOperation(value = "后台模糊查询",notes = "根据博客属性查询博客")
    @ResponseBody
    public ResponseData fuzzyBlog(String attribute,Model model){
        List<User> fuzzyBlogs = blogService.fuzzyBolg(attribute);
        if (fuzzyBlogs.size()!=0){
            model.addAttribute("fuzzyBlogs",fuzzyBlogs);
            return ResponseData.success("查询成功",fuzzyBlogs);
        }else{
            return ResponseData.fail("无符合要求的用户");
        }
    }

}
